package com.zhiteng.utils.common;

import java.util.List;

import javax.servlet.ServletOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.zhiteng.vo.AdjustAndLeaveAndSub;

public class ExcleImpl {

	public void export(String[] titles, ServletOutputStream out, List<AdjustAndLeaveAndSub> adjustAndLeaveAndSubList)
			throws Exception {
		try {
			// 第一步，创建一个workbook，对应一个Excel文件
			HSSFWorkbook workbook = new HSSFWorkbook();

			// 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
			HSSFSheet hssfSheet = workbook.createSheet("sheet1");

			// 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short

			HSSFRow row = hssfSheet.createRow(0);
			// 第四步，创建单元格，并设置值表头 设置表头居中
			HSSFCellStyle hssfCellStyle = workbook.createCellStyle();

			// 居中样式
			hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

			HSSFCell hssfCell = null;
			for (int i = 0; i < titles.length; i++) {
				hssfCell = row.createCell(i);// 列索引从0开始
				hssfCell.setCellValue(titles[i]);// 列名1
				hssfCell.setCellStyle(hssfCellStyle);// 列居中显示
			}

			for (int i = 0; i < adjustAndLeaveAndSubList.size(); i++) {
				row = hssfSheet.createRow(i + 1);
				AdjustAndLeaveAndSub sub = adjustAndLeaveAndSubList.get(i);
				// 第六步，创建单元格，并设置值
				String teacherName = null;
				if (sub.getTeacher_name() != null) {
					teacherName = sub.getTeacher_name();
				}
				row.createCell(0).setCellValue(teacherName);

				Integer AdjustNum = 0;
				if (sub.getAdjustNum() != null) {
					AdjustNum = sub.getAdjustNum();
				}
				row.createCell(1).setCellValue(AdjustNum);

				Integer Petitioner_adjustNum = 0;
				if (sub.getPetitioner_adjustNum() != null) {
					Petitioner_adjustNum = sub.getPetitioner_adjustNum();
				}
				row.createCell(2).setCellValue(Petitioner_adjustNum);

				Integer SubstituteNum = 0;
				if (sub.getSubstituteNum() != null) {
					SubstituteNum = sub.getSubstituteNum();
				}
				row.createCell(3).setCellValue(SubstituteNum);

				Integer Petitioner_substituteNum = 0;
				if (sub.getPetitioner_substituteNum() != null) {
					Petitioner_substituteNum = sub.getPetitioner_substituteNum();
				}
				row.createCell(4).setCellValue(Petitioner_substituteNum);

				Integer Total_course = 0;
				if (sub.getTotal_course() != null) {
					Total_course = sub.getTotal_course();
				}
				row.createCell(5).setCellValue(Total_course);

				// 第七步，将文件输出到客户端浏览器
				try {
					workbook.write(out);
					out.flush();
					out.close();

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new Exception("导出信息失败！");
		}
	}
}
